//
//  ProductModel.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/15.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "ProductModel.h"
#import "UploadData.h"
@implementation ProductModel
+(NSString *)getTableName
{
    return @"qbm_product";
}

+(void)columnAttributeWithProperty:(LKDBProperty *)property
{
    if([property.sqlColumnName isEqualToString:@"modelNo"])
    {
        property.isUnique = YES;
    }
    else if([property.sqlColumnName isEqualToString:@"isDel"])
    {
        property.defaultValue = @"0";
    }
    else if([property.sqlColumnName isEqualToString:@"isImageUpload"])
    {
        property.defaultValue = @"0";
    }
}


-(void)setCategoryId:(NSInteger)categoryId
{
    _categoryId=categoryId;
    _category=[CategoryModel searchSingleWithWhere:@{@"categoryId":[ToolUtils numberToStr:categoryId]} orderBy:nil];
}



+(NSString *)getPrimaryKey
{
    return @"productId";
}



+(void)dbDidInserted:(NSObject*)entity result:(BOOL)result
{
    if([entity isKindOfClass:[ProductModel class]])
    {
        ProductModel *model = (ProductModel *)entity;
        UploadData *data = [[UploadData alloc] init];
        data.t_table = [ProductModel getTableName];
        data.t_opType = DB_INSERT;
        data.t_pkName = [self getPrimaryKey];
        data.t_pk = model.rowid;
        data.t_time = [[NSDate date] timeIntervalSince1970InMilliSecond];
        model.productId = data.t_pk;
        NSMutableDictionary *params = model.keyValues;
        [params removeObjectForKey:@"category"];
        NSString *value = [self DataTOjsonString:params];
        data.content = value;
        [UploadData insertToDB:data];
    }
 //   NSLog(@"DB 插入数据成功");
    
    
}

+(void)dbDidUpdated:(NSObject*)entity result:(BOOL)result
{
    if([entity isKindOfClass:[ProductModel class]])
    {
        ProductModel *model = (ProductModel *)entity;
        UploadData *data = [[UploadData alloc] init];
        data.t_table = [ProductModel getTableName];
        data.t_opType = DB_UPDATE;
        data.t_pkName = [self getPrimaryKey];
        data.t_pk = model.rowid;
        data.t_time = [[NSDate date] timeIntervalSince1970InMilliSecond];
        model.productId = data.t_pk;
        NSMutableDictionary *params = model.keyValues;
        [params removeObjectForKey:@"category"];
        NSString *value = [self DataTOjsonString:params];
        data.content = value;
        [UploadData insertToDB:data];
    }
 //   NSLog(@"DB 更新数据");
}


+(void)dbDidDeleted:(NSObject*)entity result:(BOOL)result
{
    if([entity isKindOfClass:[ProductModel class]])
    {
        ProductModel *model = (ProductModel *)entity;
        UploadData *data = [[UploadData alloc] init];
        data.t_table = [ProductModel getTableName];
        data.t_opType = DB_DELETE;
        data.t_pkName = [self getPrimaryKey];
        data.t_pk = model.rowid;
        data.t_time = [[NSDate date] timeIntervalSince1970InMilliSecond];
        model.productId = data.t_pk;
        NSMutableDictionary *params = model.keyValues;
        [params removeObjectForKey:@"category"];
        NSString *value = [self DataTOjsonString:params];
        data.content = value;
        [UploadData insertToDB:data];
    }
  //  NSLog(@"DB 删除数据");
}

@end
